package com.dhcc.android.base.utils.data;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;


import com.dhcc.android.base.utils.Constants;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;

/**
 * 调查对象任务完成情况上报接口
 *
 * @author zhoudan
 * @date 2015年7月16日
 */
public class UploadFinish {

    private String url = "/HttpInterFace/TaskBagSate.ashx";

    private String result;

    private String TASKPACKAGEID;

    private String SURVEYID;

    private String TASKSTATE;

    /**
     * 构造方法
     * @param TASKPACKAGEID 任务包ID
     * @param SURVEYID 调查对象ID
     * @param TASKSTATE 完成数量
     */
    public UploadFinish(Activity activity,String TASKPACKAGEID,String SURVEYID,String TASKSTATE){
        this.TASKPACKAGEID = TASKPACKAGEID;
        this.SURVEYID = SURVEYID;
        this.TASKSTATE = TASKSTATE;


        if(Constants.isNetworkAvailable(activity)){
            /**
             * 上报
             */
            new Thread(upload).start();

        }else{
            Toast.makeText(activity, "网络不可用，无法上报", Toast.LENGTH_SHORT).show();
        }
    }

    private String exce;
    /**
     * 完成情况上报
     */
    Runnable upload = new Runnable() {
        private void sendMsg(int what) {
            Message msg = new Message();
            msg.what = what;
            resultHandler.sendMessage(msg);
        }

        @Override
        public void run() {
            System.out.println("###调查对象任务完成情况上报");

//            HttpClient client = new DefaultHttpClient();
//            HttpPost request;
//            try{
//                String md5 = Encrypt.encodeMD5(Constants.USERNAME + Constants.KEY);
//                md5 = md5.toUpperCase();
//                String path = "http://"+Constants.host+":"+Constants.port+ url + "?USERNAME="+Constants.USERNAME+"&TASKPACKAGEID="+TASKPACKAGEID
//                        +"&SURVEYID=" + SURVEYID
//                        +"&SURVEYUSER="+Constants.USERNAME
//                        +"&TASKSTATE=" + TASKSTATE
//                        +"&MD5="+md5;
//                System.out.println("###url ："+path);
//
//                request = new HttpPost(new URI(path));
//
//                HttpResponse response = client.execute(request);
//                if(response.getStatusLine().getStatusCode() == 200){
//                    System.out.println("###连接成功");
//                    HttpEntity entity = response.getEntity();
//                    if(entity != null){
//                        System.out.println("###获取返回数据");
//                        result = EntityUtils.toString(entity);
//                        System.out.println("####"+result);
//                        sendMsg(0);
//                    }else{
//                        exce = "返回数据为空";
//                        sendMsg(2);
//                    }
//                }else{
//                    exce = "服务器连接失败";
//                    System.out.println("###服务器连接失败");
//                    sendMsg(1);
//                }
//
//            }catch(Exception e){
//                e.printStackTrace();
//                exce = "服务器连接出错"+e.getMessage();
//                System.out.println("###发生异常");
//                sendMsg(1);
//            }
        }
    };


    Handler resultHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    analysis();
                    break;
                case 1:
                    System.out.println("###完成情况上报--"+exce);
                    break;
                case 2:
                    System.out.println("###完成情况上报--"+exce);
                    break;

                default:
                    break;
            }
        }
    };

    private void analysis(){
        try {
            JSONObject obj = new JSONObject(result);
            String code = obj.getString("RETCODE");
            if(code.equals(Constants.success)){
                System.out.println("###完成情况上报--成功");
            }else{
                System.out.println("###完成情况上报--失败");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}